package net.pulseproject.commons.util;

import java.awt.Dimension;
import java.awt.Point;

public final class StringConverterUtil {
	
	private static final String POINT_SEPARATOR = "/";
	
	private static final String DIMENSION_SEPARATOR = "x";

	private StringConverterUtil() {
		// util classes should not be instantiated
	}

	/**
	 * @param raw e.g.: x and y, "10/20"
	 */
	public static Point parseRawPoint(final String raw) {
		final int[] xAndY = StringUtil.splitTwoDigitsBySeparator(raw, POINT_SEPARATOR);
		final int x = xAndY[0];
		final int y = xAndY[1];
		return new Point(x, y);
	}
	
	/**
	 * @return e.g.: x and y, "10/20"
	 */
	public static String formatPointRaw(final Point point) {
		return StringConverterUtil.concatenateAndRound(POINT_SEPARATOR, point.getX(), point.getY());
	}

	/**
	 * @param raw e.g.: width times height, e.g.: "800x600"
	 */
	public static Dimension parseRawDimension(final String raw) {
		final int[] xAndY = StringUtil.splitTwoDigitsBySeparator(raw, DIMENSION_SEPARATOR);
		final int x = xAndY[0];
		final int y = xAndY[1];
		return new Dimension(x, y);
	}
	
	/**
	 * @return width times height, e.g.: "800x600"
	 */
	public static String formatDimensionRaw(final Dimension dimension) {
		return StringConverterUtil.concatenateAndRound(DIMENSION_SEPARATOR,
				dimension.getWidth(), dimension.getHeight());
	}
	
	private static String concatenateAndRound(final String separator, final double digit1, final double digit2) {
		return Math.round(digit1) + separator + Math.round(digit2); 
	}
}
